package com.tensquare.spit.controller;

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SpitController
 * @Description TODO
 * @Author zhangshuai
 * @Date 2019/1/15 21:35
 * @Version 1.0
 **/

@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;

    /**
     * 查询所有记录
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List<Spit> list = spitService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据id查询
     *
     * @param spitId
     * @return
     */
    @RequestMapping(value = "/{spitId}", method = RequestMethod.GET)
    public Result findById(@PathVariable String spitId) {
        return new Result(true, StatusCode.OK, "查询成功", spitService.findById(spitId));
    }

    /**
     * 增加记录
     *
     * @param spit
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Spit spit) {

        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null) {
            return new Result(false, StatusCode.ACCESSERROR, "无权访问");
        }
        spit.setUserid(claims.getId());
        spitService.save(spit);
        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改成功
     *
     * @param spit
     * @return
     */
    @RequestMapping(value = "/{spitId}", method = RequestMethod.PUT)
    public Result update(@RequestBody Spit spit) {
        spitService.update(spit);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除记录
     *
     * @param spitId
     * @return
     */
    @RequestMapping(value = "/{spitId}", method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String spitId) {
        spitService.deleteById(spitId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 根据父id查询上级
     *
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/comment/{parentid}/{page}/{size}", method = RequestMethod.GET)
    public Result findByParentId(@PathVariable String parentid, @PathVariable int page, @PathVariable int size) {
        Page<Spit> page1 = spitService.findByParentId(parentid, page, size);
        return new Result(true, StatusCode.OK, "查询上级成功", new PageResult<>(page1.getTotalElements(), page1.getContent()));
    }

    /**
     * 吐槽点赞
     *
     * @param spitId
     * @return
     */
    @RequestMapping(value = "/thumbup/{spitId}", method = RequestMethod.PUT)
    public Result updateThumbup(@PathVariable String spitId) {

        //判断用户是否点过赞
        String userid = "2023";
        if (redisTemplate.opsForValue().get("thumbup_" + userid + "_" + spitId) != null) {

            return new Result(false, StatusCode.REPERROR, "已经点过了...");
        }
        spitService.updateThumbup(spitId);
        redisTemplate.opsForValue().set("thumbup_" + userid + "_" + spitId, "1");
        return new Result(true, StatusCode.OK, "点赞成功");
    }

    /**
     * 吐槽浏览量
     *
     * @param spitId
     * @return
     */
    @RequestMapping(value = "/visits/{spitId}", method = RequestMethod.PUT)
    public Result updateVisits(@PathVariable String spitId) {

        spitService.updateVisits(spitId);
        return new Result(true, StatusCode.OK, "浏览成功");
    }

    /**
     * 吐槽分享数
     *
     * @param spitId
     * @return
     */
    @RequestMapping(value = "/share/{spitId}", method = RequestMethod.PUT)
    public Result updateShare(@PathVariable String spitId) {

        spitService.updateShare(spitId);
        return new Result(true, StatusCode.OK, "浏览成功");
    }


}
